package com.ruoyi.web.controller.energy;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.energy.domain.EmsAlarmRule;
import com.ruoyi.energy.service.IEmsAlarmRuleService;

/**
 * 告警规则Controller
 * 
 * @author ruoyi
 * @date 2024-01-20
 */
@RestController
@RequestMapping("/energy/alarm/rule")
public class EmsAlarmRuleController extends BaseController {
    
    @Autowired
    private IEmsAlarmRuleService emsAlarmRuleService;

    /**
     * 查询告警规则列表
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmsAlarmRule emsAlarmRule) {
        startPage();
        List<EmsAlarmRule> list = emsAlarmRuleService.selectEmsAlarmRuleList(emsAlarmRule);
        return getDataTable(list);
    }

    /**
     * 查询有效告警规则列表
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:list')")
    @GetMapping("/active")
    public AjaxResult getActiveRules() {
        List<EmsAlarmRule> list = emsAlarmRuleService.selectActiveAlarmRuleList();
        return AjaxResult.success(list);
    }

    /**
     * 导出告警规则列表
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:export')")
    @Log(title = "告警规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsAlarmRule emsAlarmRule) {
        List<EmsAlarmRule> list = emsAlarmRuleService.selectEmsAlarmRuleList(emsAlarmRule);
        ExcelUtil<EmsAlarmRule> util = new ExcelUtil<EmsAlarmRule>(EmsAlarmRule.class);
        util.exportExcel(response, list, "告警规则数据");
    }

    /**
     * 获取告警规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId) {
        return AjaxResult.success(emsAlarmRuleService.selectEmsAlarmRuleByRuleId(ruleId));
    }

    /**
     * 新增告警规则
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:add')")
    @Log(title = "告警规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsAlarmRule emsAlarmRule) {
        // 校验规则名称唯一性
        String checkResult = emsAlarmRuleService.checkRuleNameUnique(emsAlarmRule);
        if (!"0".equals(checkResult)) {
            return AjaxResult.error(checkResult);
        }
        
        return toAjax(emsAlarmRuleService.insertEmsAlarmRule(emsAlarmRule));
    }

    /**
     * 修改告警规则
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:edit')")
    @Log(title = "告警规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsAlarmRule emsAlarmRule) {
        // 校验规则名称唯一性
        String checkResult = emsAlarmRuleService.checkRuleNameUnique(emsAlarmRule);
        if (!"0".equals(checkResult)) {
            return AjaxResult.error(checkResult);
        }
        
        return toAjax(emsAlarmRuleService.updateEmsAlarmRule(emsAlarmRule));
    }

    /**
     * 删除告警规则
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:remove')")
    @Log(title = "告警规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds) {
        return toAjax(emsAlarmRuleService.deleteEmsAlarmRuleByRuleIds(ruleIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('energy:alarm:edit')")
    @Log(title = "告警规则", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody EmsAlarmRule emsAlarmRule) {
        return toAjax(emsAlarmRuleService.updateAlarmRuleStatus(emsAlarmRule));
    }

    /**
     * 校验规则名称
     */
    @GetMapping("/checkRuleNameUnique")
    public AjaxResult checkRuleNameUnique(EmsAlarmRule emsAlarmRule) {
        String result = emsAlarmRuleService.checkRuleNameUnique(emsAlarmRule);
        return AjaxResult.success("0".equals(result));
    }
} 